<?php
/**
 * @author yupoxiong<i@yufuping.com>
 */

namespace generate\field;

class File extends Field
{

    public static $html = <<<EOF
    <div class="layui-form-item">
    <label for="[FIELD_NAME]" class="layui-form-label [REQUIRED]">[FORM_NAME]</label>
    <div class="layui-input-inline">
        <div class="layui-btn" id="[FIELD_NAME]"> 上传[FORM_NAME]</div>
        <div class="layui_img">
            <div class="layui-progress upload_progress " id="[FIELD_NAME]_progress" lay-showPercent="yes"
                lay-filter="progressBar[FIELD_NAME]">
                <div class="layui-progress-bar layui-bg-red" lay-percent="0%"></div>
            </div>
            <div {if !isset(\$data.[FIELD_NAME])  ||\$data.[FIELD_NAME]=='' } style="display: none;" {/if} id="form[FIELD_NAME]" >
                {\$data.[FIELD_NAME]|default=''}
            </div>
            <input type="hidden"  lay-verify="[FORM_VALIDATE]"  name="[FIELD_NAME]" id="input[FIELD_NAME]"
                value="{\$data.[FIELD_NAME]|default=''}">
        </div>
    </div>
    <script>
        layui.use('upload', function () {
            var upload = layui.upload;
            upload.render({
                elem: '#[FIELD_NAME]'
                , url: "{:url('upfile/upload')}"
                , size: 10240
                , progress: function (n, elem, res, index) {
                    $('#[FIELD_NAME]_progress').show();
                    var percent = n + '%'
                    element.progress('progressBar[FIELD_NAME]', percent);
                }
                , done: function (res, index, upload) {
                    if (res.code == 1) {
                        $("#input[FIELD_NAME]").val(res.data.url);
                        $('#[FIELD_NAME]_progress').hide();
                        $("#form[FIELD_NAME]").text(res.data.url).show();
                    } else {
                        layer.msg(res.msg, { icon: 2, time: 2000 });
                        element.progress('progressBar[FIELD_NAME]', '0%');
                        $('#[FIELD_NAME]_progress').hide();
                    }
                }
            });
        })
    </script>
</div>\n
EOF;



    public static $rules = [
        'required'  => '非空',
        'file_size' => '文件大小限制',
        'file_ext'  => '文件后缀限制',
        'regular'   => '自定义正则'
    ];




    public static function create($data)
    {
        $html = self::$html;
        $html = str_replace(array('[FORM_NAME]', '[FIELD_NAME]', '[FORM_VALIDATE]', '[REQUIRED]'), array($data['form_name'], $data['field_name'], self::$verify, self::$required), $html);
        return $html;
    }
}
